<?php

/* 
 * Copyright (c) 2009 Nguyen Duc Thuan <me@ndthuan.com>
 * All rights reserved.
 */

class Fete_View_Helper_Pagination extends Fete_View_Helper
{
    protected $_pageNum     = 1;
    protected $_totalPages  = 0;
    protected $_numRange    = 4;
    protected $_urlTemplate = '';
    protected $_header      = '';
    protected $_footer      = '';
    protected $_beforeLink  = ' ';
    protected $_afterLink   = ' ';
    protected $_prevText    = '&lt;';
    protected $_nextText    = '&gt;';

    /**
     *
     * @param integer $pageNum
     * @param integer $totalPages
     * @param string $urlTemplate
     * @return Fete_View_Helper_Pagination
     */
    public function &pagination($pageNum, $totalPages, $urlTemplate)
    {
        $this->_pageNum     = $pageNum;
        $this->_totalPages  = $totalPages;
        $this->_urlTemplate = $urlTemplate;

        return $this;
    }

    /**
     *
     * @param integer $range
     * @return Fete_View_Helper_Pagination
     */
    public function &setNumRange($range)
    {
        $this->_numRange = $range;

        return $this;
    }

    /**
     *
     * @param string $header
     * @return Fete_View_Helper_Pagination
     */
    public function &setHeader($header)
    {
        $this->_header = $header;

        return $this;
    }

    /**
     *
     * @param string $footer
     * @return Fete_View_Helper_Pagination
     */
    public function &setFooter($footer)
    {
        $this->_footer = $footer;

        return $this;
    }

    /**
     *
     * @param string $text
     * @return Fete_View_Helper_Pagination
     */
    public function &setBeforeLink($text)
    {
        $this->_beforeLink = $text;

        return $this;
    }

    /**
     *
     * @param string $text
     * @return Fete_View_Helper_Pagination
     */
    public function &setAfterLink($text)
    {
        $this->_afterLink = $text;

        return $this;
    }

    /**
     *
     * @param string $text
     * @return Fete_View_Helper_Pagination
     */
    public function &setPrevText($text)
    {
        $this->_prevText = $text;

        return $this;
    }

    /**
     *
     * @param string $text
     * @return Fete_View_Helper_Pagination
     */
    public function &setNextText($text)
    {
        $this->_nextText = $text;

        return $this;
    }

    public function __toString()
    {
        ob_start();
        if ($this->_totalPages > 1) {

            $firstPage  = $this->_pageNum - $this->_numRange;
            $lastPage   = $this->_pageNum + $this->_numRange;

            if ($firstPage < 1) {
                $firstPage = 1;
            }

            if ($lastPage > $this->_totalPages) {
                $lastPage = $this->_totalPages;
            }

            echo $this->_header;

            if ($this->_pageNum > 1) {
                echo $this->_beforeLink
                , '<a href="'
                , str_replace('$page$'
                    , intval($this->_pageNum - 1), $this->_urlTemplate)
                , '">'
                , $this->_prevText
                , '</a>'
                , $this->_afterLink;
            }

            if ($firstPage > 1) {
                echo $this->_beforeLink
                , '<a href="'
                , str_replace('$page$', '1', $this->_urlTemplate)
                , '">1</a>'
                , $this->_afterLink
                , $this->_beforeLink
                , '...'
                , $this->_afterLink;
            }

            for ($counter = $firstPage; $counter <= $lastPage; ++$counter)
            {
                echo $this->_beforeLink
                , '<a'
                , ($counter == $this->_pageNum
                    ? ' class="current"' : '')
                , ' href="'
                , str_replace('$page$', $counter, $this->_urlTemplate)
                , '">', $counter
                , '</a>'
                , $this->_afterLink;
            }

            if ($lastPage < $this->_totalPages) {
                echo $this->_beforeLink
                , '...'
                , $this->_afterLink
                , $this->_beforeLink
                , '<a href="'
                , str_replace('$page$', $this->_totalPages, $this->_urlTemplate)
                , '">'
                , $this->_totalPages
                , '</a>'
                , $this->_afterLink;
            }

            if ($this->_pageNum < $this->_totalPages) {
                echo $this->_beforeLink
                , '<a href="'
                , str_replace('$page$', intval($this->_pageNum + 1)
                    , $this->_urlTemplate)
                , '">'
                , $this->_nextText
                , '</a>'
                , $this->_afterLink;
            }

            echo $this->_footer;
        }

        return ob_get_clean();
    }
}